const fp = require('lodash/fp')

class Container {
  static of(value) {
    return new Container(value)
  }

  constructor(value) {
    this._value = value
  }

  map(fn) {
    return Container.of(fn(this._value));
  }
}

class Maybe {
  static of(value) {
    return new Maybe(value)
  }

  isNothing() {
    console.log(this._value,"Maybe")  
    return this._value === null || this._value === undefined;
  }

  constructor(value) {
    this._value = value
  }

  map(fn) {
    console.log(fn,"Maybe")  
    return this.isNothing() ? this : Maybe.of(fn(this._value));
  }
}

/*
  练习1: 
  实现函数 ex1 
  使用 fp.add(x, y) 和 fp.map(f, x)
  让函子里的值 增加1
*/
// 1.创建一个函子
let maybe = Maybe.of([5, 6, 1])

// 2.实现 ex1 函数

// 3.调用测试
// console.log( maybe.map(ex1) )  // Maybe { _value: [ 6, 7, 2 ] }
function ex1(data) {
  let map = (arr) => {
    return fp.map(x => fp.add(x, 1), arr)
  }
  return map(data)
}

console.log(maybe.map(ex1) )
